package com.company.cloud.web.utils;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.company.cloud.web.models.SitMapModel;
import org.springframework.util.StringUtils;

import java.math.BigDecimal;
import java.util.*;

public class WebPrdUtils {

    public static JSONObject getProductInfo(String productNo) {
        Map<String, Object> paramMap = new HashMap<>();
        paramMap.put("productNo", productNo);
        paramMap.put("isMarketable", "1");
//        String body= HttpUtil.get(WebUrlUtils.getUrl(WebUrlUtils.Prd_Get_Url),paramMap);
        String body = HttpRequest.get(WebUrlUtils.getUrl(WebUrlUtils.Prd_Get_Url))
                .header("tenant", "PTC")
                .form(paramMap)
                .execute().body();
        if (StrUtil.isEmpty(body)) {
            return null;
        }
        JSONObject product = JSON.parseObject(body);
        return product;
    }


    public static JSONObject getPrdRecommendList(Map<String, String> params) {
        Map<String, Object> paramMap = new HashMap<>();
        if (StrUtil.isNotEmpty(params.get("categoryId"))) {
            paramMap.put("categoryId", Integer.parseInt(params.get("categoryId")));
        }
        if (StrUtil.isNotEmpty(params.get("num"))) {
            paramMap.put("num", Integer.parseInt(params.get("num")));
        } else {
            paramMap.put("num", 10);
        }
        if (StrUtil.isNotEmpty(params.get("bizValue10"))) {
            paramMap.put("bizValue10", params.get("bizValue10"));
        }
        if (StrUtil.isNotEmpty(params.get("bizValue11"))) {
            paramMap.put("bizValue11", params.get("bizValue11"));
        }
        if (StrUtil.isNotEmpty(params.get("bizValue12"))) {
            paramMap.put("bizValue12", params.get("bizValue12"));
        }
        String body = HttpRequest.post(WebUrlUtils.getUrl(WebUrlUtils.Prd_Recommend_Url))
                .body(JSON.toJSONString(paramMap)).execute().body();
        if (StrUtil.isEmpty(body)) {
            return null;
        }
        JSONObject product = JSON.parseObject(body);
        return product;
    }

    public static JSONObject queryProduct(Map<String, String> params) {
        Map<String, Object> paramMap = new HashMap<>();
        if (StrUtil.isNotEmpty(params.get("page"))) {
            paramMap.put("page", Integer.parseInt(params.get("page")));
        }
        if (StrUtil.isNotEmpty(params.get("rows"))) {
            paramMap.put("rows", Integer.parseInt(params.get("rows")));
        }

        if (StrUtil.isNotEmpty(params.get("price"))) {
            String price = params.get("price");
            String[] prices = price.split("-");
            if (prices.length == 2) {
                paramMap.put("priceGe", new BigDecimal(prices[0]));
                paramMap.put("priceLe", new BigDecimal(prices[1]));
            } else {
                paramMap.put("priceGe", new BigDecimal(prices[0]));
            }
        }

        if (StrUtil.isNotEmpty(params.get("bizValue6"))) {
            String size = params.get("bizValue6");
            String[] sizes = size.split("-");
            if (sizes.length == 2) {
                paramMap.put("bizValue6Ge", sizes[0]);
                paramMap.put("bizValue6Le", sizes[1]);
            } else {
                paramMap.put("bizValue6Ge", sizes[0]);
            }
        }

        if (StrUtil.isNotEmpty(params.get("category"))) {
            paramMap.put("categoryId", Integer.parseInt(params.get("category")));
        }
        if (StrUtil.isNotEmpty(params.get("subCategoryId"))) {
            paramMap.put("subCategoryId", params.get("subCategoryId"));
        }

        if (StrUtil.isNotEmpty(params.get("px_time"))) {
            paramMap.put("orderByDate", params.get("px_time"));
        }
        if (StrUtil.isNotEmpty(params.get("px_price"))) {
            paramMap.put("orderByPrice", params.get("px_price"));
        }

        if (StrUtil.isNotEmpty(params.get("bizValue14"))) {
            paramMap.put("bizValue14", params.get("bizValue14"));
        }
        if (StrUtil.isNotEmpty(params.get("bizValue8"))) {
            paramMap.put("bizValue8", params.get("bizValue8"));
        }
        if (StrUtil.isNotEmpty(params.get("bizValue7"))) {
            paramMap.put("bizValue7", params.get("bizValue7"));
        }
        if (StrUtil.isNotEmpty(params.get("bizValue11"))) {
            paramMap.put("bizValue11", params.get("bizValue11"));
        }
        if (StrUtil.isNotEmpty(params.get("bizValue12"))) {
            paramMap.put("bizValue12", params.get("bizValue12"));
        }
        if (StrUtil.isNotEmpty(params.get("bizValue19"))) {
            paramMap.put("bizValue19", params.get("bizValue19"));
        }
        if (StrUtil.isNotEmpty(params.get("name"))) {
            paramMap.put("name", params.get("name"));
        }

        if (StrUtil.isNotEmpty(params.get("companyNo"))) {
            paramMap.put("companyNo", params.get("companyNo"));
        }
        paramMap.put("isMarketable", "1");

        String body = HttpRequest.post(WebUrlUtils.getUrl(WebUrlUtils.Prd_Query_Url))
                .body(JSON.toJSONString(paramMap)).execute().body();
        JSONObject product = JSON.parseObject(body);
        return product;

    }

    public static JSONObject categoryTree() {
        String body = HttpRequest.get(WebUrlUtils.getUrl(WebUrlUtils.PrdCategory_Get_Url))
                .header("tenant", "PTC")
                .execute().body();
        if (StringUtils.isEmpty(body)) {
            return null;
        }
        JSONObject productCategory = JSON.parseObject(body);
        return productCategory;
    }


    public static Map<String, List<SitMapModel>> getSitMap(String path) {
        String body = HttpRequest.get(WebUrlUtils.getUrl(WebUrlUtils.Prd_SitMap_Url)).execute().body();
        if (StrUtil.isEmpty(body)) {
            return null;
        }
        JSONObject siteMap = JSON.parseObject(body);

        JSONObject jsonData = siteMap.getJSONObject("data");
        if (jsonData == null) {
            return null;
        }

        Map<String, List<SitMapModel>> data = new HashMap<>();
        String lastDate = DateUtil.format(new Date(), "yyyy-MM-dd");

        JSONArray artist = jsonData.getJSONArray("artist");
        if (artist != null) {
            List<SitMapModel> artistList = new ArrayList<>();
            for (int i = 0; i < artist.size(); i++) {
                Object obj = artist.get(i);
                if (obj != null) {
                    artistList.add(new SitMapModel((String) obj, lastDate, path));
                }
            }
            data.put("artist", artistList);
        }

        JSONArray prd = jsonData.getJSONArray("prd");
        if (prd != null) {
            List<SitMapModel> prdList = new ArrayList<>();
            for (int i = 0; i < prd.size(); i++) {
                Object obj = prd.get(i);
                if (obj != null) {
                    prdList.add(new SitMapModel((String) obj, lastDate, path));
                }
            }
            data.put("prd", prdList);
        }

        JSONArray news = jsonData.getJSONArray("news");
        if (news != null) {
            List<SitMapModel> newsList = new ArrayList<>();
            for (int i = 0; i < news.size(); i++) {
                Object obj = news.get(i);
                if (obj != null) {
                    newsList.add(new SitMapModel((String) obj, lastDate, path));
                }
            }
            data.put("news", newsList);
        }

        JSONArray organ = jsonData.getJSONArray("organ");
        if (organ != null) {
            List<SitMapModel> organList = new ArrayList<>();
            for (int i = 0; i < organ.size(); i++) {
                Object obj = organ.get(i);
                if (obj != null) {
                    organList.add(new SitMapModel((String) obj, lastDate, path));
                }
            }
            data.put("organ", organList);
        }

        return data;
    }


}
